// pages/CreateGroup/CreateGroup.js
const app = getApp()
var lunbotuimg = [];
var arrimgs = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //长按事件
    touchStartTime: 0,    // 触摸开始时间
    touchEndTime: 0,      // 触摸结束时间
    textIndex:false,
    array_area: [
      { id: 1, name: '市级' },
      { id: 2, name: '增城区' },
      { id: 3, name: '从化区' }
    ],
    index_area: 0,
    img_ids: [],          //上传图片的id
    img_count: 1,         //目前可以上传图片的数量
    addLable:true,
    labelInputVal:'',
    uploaderImg: "/images/upload.png",
    btnarr: [],
    choosebtnarr:[],
    theLabel:true,
    biaozhiimg:'',
    shenfenz:'',
    theradio:'企业',
    lunbotu:[],
    lunbotuimg:'',
    userdata:'',
    theradio1: false, 
    theradio2: true 
  },

  selectImg: function (e) {
    let that = this;
    if (e.currentTarget.id=="biaozhi"){
      var data = { count: 1 }
      app.upImg(data, function (res) {
        that.setData({
          biaozhiimg: res
        });
      })
    } else if (e.currentTarget.id == "shenfenz") {
      var data = { count: 1 }
      app.upImg(data, function (res) {
        that.setData({
          shenfenz: res
        });
      })
    } else if (e.currentTarget.id == "lunbotu") {
      var data = { count: 3 }
      app.upImg(data, function (res) {
        arrimgs.push(res)
        lunbotuimg.push(res.imageURL)
        console.log(lunbotuimg)
        that.setData({
          lunbotu: arrimgs,
          lunbotuimg: lunbotuimg
        });
      })
    }
  },

  bindPickerChangeArea: function (e) {
    let that = this;
    let index = e.detail.value;
    // let array_area_id = that.data.array_area[index].id;
    that.setData({
      index_area: index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.btnarr.forEach(item=>{item.checked = false})
    this.setData({
      btnarr: this.data.btnarr
    })
    console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      userdata: wx.getStorageSync("userdata")
    })
    var data={
      paraUrl: "label/list",
      paraData:''
    }
    app.getRequest(data,function(res){
      if (res.data.result == 200) {
        that.setData({
          btnarr: res.data.body
        })
      } else {
      }
    })
    
  },

  // 按钮触摸开始触发的事件
  touchStart1: function (e) {
    this.touchStartTime = e.timeStamp
  },
  // 按钮触摸结束触发的事件
  touchEnd1: function (e) {
    this.touchEndTime = e.timeStamp
  },
  // 长按
  longTap1: function (e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            biaozhiimg: ''
          });
        }
      }
    })
  },

  // 按钮触摸开始触发的事件
  touchStart2: function (e) {
    this.touchStartTime = e.timeStamp
  },
  // 按钮触摸结束触发的事件
  touchEnd2: function (e) {
    this.touchEndTime = e.timeStamp
  },
  // 长按
  longTap2: function (e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            shenfenz: ''
          });
        }
      }
    })
  },

  // 按钮触摸开始触发的事件
  touchStart3: function (e) {
    this.touchStartTime = e.timeStamp
  },
  // 按钮触摸结束触发的事件
  touchEnd3: function (e) {
    this.touchEndTime = e.timeStamp
  },
  // 长按
  longTap3: function (e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除？',
      success: function (res) {
        if (res.confirm) {
          let lunbotu = that.data.lunbotu;
          let lunbotuimg = that.data.lunbotuimg;
          //数组下标
          let index = e.currentTarget.dataset.index;
          lunbotu.splice(index, 1);
          lunbotuimg.splice(index, 1);
          that.setData({
            lunbotu: arrimgs,
            lunbotuimg: lunbotuimg
          });
        }
      },
      fail: function () {
        wx.hideLoading();
        app.requestError();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //点击保存
  formBindsubmit: function (e) {
    var that = this,labels=[];
    for (var i = 0; i < that.data.choosebtnarr.length;i++){
      var labelsObj = {
        "id": that.data.choosebtnarr[i].id,
        "code": that.data.choosebtnarr[i].code,
        "name": that.data.choosebtnarr[i].name
      }
      labels.push(labelsObj)
    }
    var formwhere = wx.getStorageSync("formwhere")
    if (formwhere == 1) {
      var brand = false
    } else if (formwhere == 2) {
      var brand = true
    }
    var paraData = {
      "name": e.detail.value.name, // 群组名称
      "desc": e.detail.value.desc, // 群组描述介绍
      "logo": that.data.biaozhiimg.imageURL, // 群组logo
      "type": that.data.theradio, // 群分类, 按页面设置常量
      "brand": brand, // 是否品牌, 否则为群组
      "organization": e.detail.value.organization, // 组织机构
      "phone": e.detail.value.phone, // 联系方式
      "labels": labels,
      "idCard": e.detail.value.idCard, // 身份证号码
      "idCardImage": that.data.shenfenz.imageURL, // 手持身份证图片
      "imageUrls": that.data.lunbotuimg // 群组轮播图
    }
    if (paraData.name == "" || paraData.desc == "" || paraData.logo == "" || paraData.type == "" || paraData.organization == "" || paraData.phone == "" || paraData.labels == "" || paraData.idCard == "" || paraData.idCardImage==""){
      wx.showToast({
        title: '请将数据填写完整',
        icon: 'success',
        image: '/images/shibai.png',
        duration: 2000
      })
      return;
    }
    var data = { paraUrl: 'group/create', paraData: paraData }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function(){
          wx.reLaunch({
            url: "../Brand/Brand"
          })
        },2000)
      } else {
        wx.showToast({
          title: '添加失败',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
    })
  },
  //显示自定义标签
  addLabelContr:function(){
    var that=this;
    that.setData({
      addLable: false
    })
  },
  // 获取输入框的值
  getInput: function (e) {
    this.setData({
      labelInputVal: e.detail.value
    })
  },
  //新增标签
  addFun:function(){
    var that=this;
    if (that.data.labelInputVal == ""){
      wx.showToast({
        title: "请输入标签名",
        icon: 'success',
        image: '/images/shibai.png',
        duration: 2000
      })
      return;
    }
    var data = {
      paraUrl: "label/create?name=" + that.data.labelInputVal,
      paraData: ''
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })
        that.onShow();
      } else if (res.data.result==60009 ){
        wx.showToast({
          title: '此标签已经存在',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '添加失败',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
    })
  },
  // 选择标签
  changebtn(e) {
    let index = e.currentTarget.dataset.index
    if (this.data.choosebtnarr.length > 7){
      wx.showToast({
        title: '只能选择8个标签',
        icon: 'success',
        image: '/images/shibai.png',
        duration: 2000
      })
      return;
    }
    this.data.choosebtnarr.push(this.data.btnarr[index])
    this.setData({ choosebtnarr: this.data.choosebtnarr })
    var btnarr = app.removeItem(this.data.btnarr, e.currentTarget.dataset.bid);
    this.setData({ btnarr: btnarr })
  },
  // 移出已选择的标签
  removebtn:function(e){
    // 根据id移出标签
    var findarr = app.findItem(this.data.choosebtnarr, e.currentTarget.dataset.bid);
    var choosebtnarr=app.removeItem(this.data.choosebtnarr, e.currentTarget.dataset.bid);
    this.data.btnarr.push(findarr)
    this.setData({ choosebtnarr: choosebtnarr })
    this.setData({ btnarr: this.data.btnarr })
  },
  // 确定
  confirm:function(){
    this.setData({ theLabel: true, textIndex: false })
  },
  //取消
  cancel:function(){
    this.setData({ theLabel: true, textIndex: false })
  },
  // 触发弹出层
  showTan(){
    this.setData({ 
      theLabel: false,
      textIndex:true
    })
  },
  radioChange:function(e){
    console.log(e.detail.value)
    this.setData({ theradio: e.detail.value })
    if (e.detail.value == "企业"){
      this.setData({ theradio1: false,theradio2: true })
    } else if (e.detail.value == "社团"){
      this.setData({ theradio1: true, theradio2: false })
    } else if (e.detail.value == "个人"){
      this.setData({ theradio1: true, theradio2: true,theradio3: true })
    }
  }
})